package com.liuche.web.controller.system;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.code.kaptcha.Producer;
import com.liuche.domain.dto.system.UserInfoDto;
import com.liuche.service.erp.system.HelloWorldService;
import com.liuche.web.controller.BaseController;

@Controller
public class HelloWorldController extends BaseController {
	@Autowired
	private HelloWorldService helloWorldService;
	@Autowired
	private Producer producer;

	/**
	 * 1. ʹ��RequestMappingע����ӳ�������URL 2.
	 * ����ֵ��ͨ����ͼ����������Ϊʵ�ʵ�������ͼ,
	 * ����InternalResourceViewResolver��ͼ���������������½���
	 * ͨ��prefix+returnVal+suffix �����ķ�ʽ�õ�ʵ�ʵ�������ͼ��Ȼ���ת������
	 * "/WEB-INF/views/success.jsp"
	 * 
	 * @return
	 */
	@RequestMapping("/system/helloWorld/hello")
	public String hello() {
		System.out.println("hello world");
		return "/Views/System/HelloWorld/Hello.jsp";
	}

	@RequestMapping("/system/helloWorld/jsonDemo")
	@ResponseBody
	public Map<String, String> jsonDemo() throws JsonProcessingException, IOException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("liuche", "��");
		map.put("liuli", "Ů");
		return map;
	}

	@RequestMapping("/system/helloWorld/jsonDemo2")
	public void jsonDemo2() throws JsonProcessingException, IOException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("liuche", "��");
		map.put("liuli", "Ů");
		responseJson(jackson.writeValueAsString(map));
	}

	@RequestMapping(value = "/system/helloWorld/stringDemo", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String stringDemo() throws JsonProcessingException, IOException {
		return "����";
	}

	@RequestMapping(value = "/system/helloWorld/stringDemo2")
	public void stringDemo2() throws JsonProcessingException, IOException {
		responseContent("����");
	}

	@RequestMapping(value = "/system/helloWorld/register", method = RequestMethod.GET)
	public String register() {
		return "/Views/System/HelloWorld/Register.jsp";
	}

	@RequestMapping("/system/helloWorld/registerUser")
	public String registerUser() throws IOException {
		ServletContext context = request.getServletContext();
		context.setAttribute("name", request.getParameter("name"));
		return "/Views/System/HelloWorld/Success.jsp";
	}

	@RequestMapping(value = "/system/helloWorld/addUser", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addUser() throws Exception {
		/*helloWorldService.AddUser("wangling", "111111", "王林", "工程师", "1", null, null, "2015-02-01", "1523659854",
				"湖南岳阳", "jack", "1", "liuche");*/
		helloWorldService.AddUserBatch("wangling", "111111", "王林", "工程师", "1", null, null, "2015-02-01", "1523659854",
				"湖南岳阳", "jack", "1", "liuche");
		return "添加成功";
	}

	@RequestMapping(value = "/system/helloWorld/getUserDepartmentList", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public void getUserDepartmentList() throws IOException {
		Object[] obj = helloWorldService.getUserDepartmentList(null, null, 1, 20);
		responseJson(jackson.writeValueAsString(obj));
	}

	@RequestMapping("/system/helloWorld/getValidationImage")
	public void getValidationImage(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws IOException {
		String lastYZM = (String) session.getAttribute("yzm");
		System.out.println("*******************上一次的验证码是" + lastYZM + "**********************");
		/*
		 * // 设置浏览器不要对数据进行缓存 response.setDateHeader("Expires", 0); // Set
		 * standard HTTP/1.1 no-cache headers.
		 * response.setHeader("Cache-Control",
		 * "no-store, no-cache, must-revalidate"); // Set IE extended HTTP/1.1
		 * no-cache headers (use addHeader). response.addHeader("Cache-Control",
		 * "post-check=0, pre-check=0"); // Set standard HTTP/1.0 no-cache
		 * header. response.setHeader("Pragma", "no-cache"); // return a jpeg
		 */ response.setContentType("image/jpeg");
		String yzm = producer.createText();
		session.setAttribute("yzm", yzm);
		BufferedImage image = producer.createImage(yzm);
		OutputStream out = response.getOutputStream();
		ImageIO.write(image, "jpg", out);
		out.flush();

	}

	@RequestMapping("/system/helloWorld/jstl")
	public ModelAndView  jstl() {
		session.setAttribute("userName", "liuche");
		ModelAndView mv=new ModelAndView();
		mv.setViewName("/Views/System/HelloWorld/Jstl.jsp");
		Object[] o=helloWorldService.getUserDepartmentList(null, null, 1, 20);
		mv.addObject("list", (List<UserInfoDto>)o[0]);
		mv.addObject("count", o[1]);
		return mv;
	}
}
